'use strict';

stareal
    .controller("NewActivityListController", function ($scope, $stateParams, $api, $sce, base64, $state, $alert,localStorageService,$lazyLoader,$location,$anchorScroll) {
        $scope.mypage = 3;
        $scope.activenavbar= $stateParams.activenavbar; //0
        $scope.currentClassfyId='';
        $scope.currentIndex=$stateParams.kind;//0
        $scope.Activitystatus=false;
        $scope.noPic=false;
        $scope.cityStatus=false;
        $scope.closedActivitysAdvsStatus=false;
        if(localStorageService.get('cityid')==undefined||localStorageService.get('cityid')==null){
            $scope.cityid='';
            $scope.cityName='全国';
        }else{
            $scope.cityid=localStorageService.get('cityid');
            $scope.cityName=localStorageService.get('cityname');
        }
        $scope.goToactivedetail=function(id){
            $state.go('main.activedetail',{id:id},true);
            localStorageService.set('activeclassifyInde',  $scope.currentIndex);
            localStorageService.set('activeclassifyType', $scope.activenavbar);
        }
        $scope.getAcTivityBanner=function(){  //app/start/activityScreen
            // 分类列表
            $api.get("app/start/activityScreen",{}, true)
                .then(function(ret){
                    var data=ret.data;
                    if(data.picurl==undefined||data.picurl==''||data.picurl==null){
                        $scope.closedActivitysAdvsStatus=false;
                        $scope.noPic=true;
                    }else{
                        $scope.closedActivitysAdvsStatus=true;
                        $scope.AcTivityBanner=data;
                    }
                });
        }
        if($scope.currentIndex!=3){
            $scope.closedActivitysAdvsStatus=false;
            $scope.showActivitysBidStatus=false;
        }else {
            if (!$scope.noPic) {
                // console.log(_czc );
                // if (_czc != undefined && _czc != null && _czc != '') {
                //     _czc.push(["_trackEvent", 'h5-活动广告', '活动广告', '展示量', 'activity']);
                // }
                $scope.closedActivitysAdvsStatus=true;
                $scope.showActivitysBidStatus=true;
            }

        }
        $scope.getAbtainAidBar=function(){//app/activity/abtainAidBar  //获取应援
            // 分类列表
            $api.get("app/activity/abtainAidBar",{}, true)
                .then(function(ret){
                    $scope.abtainAidBar=ret;
                });
        }
        if(!localStorageService.get('token')){
            var data={};
            data.count=0;
            $scope.abtainAidBar=data;
        }else{
            $scope.getAbtainAidBar();
        }
        $scope.goToOther=function(url){
            if(url!=''&&url!=undefined&&url!=null){
                window.open(url);
            }
        }
        //兑吧自定义活动
        $scope.goToOtherDui=function(url){
            if(!localStorageService.get('token')){
                $scope.activeDuiDailogStatus=true;
                return false;
            }
            if(url!=''&&url!=undefined&&url!=null){
                $scope.venus360Id=url+'&isApp=1';
                $api.get("app/duiba/shareH5",{dbredirect:$scope.venus360Id},true)
                    .then(function (ret) {
                        var data=ret.data;
                        console.log(data);
                        var link=data.url;
                        console.log(link);
                        window.location.href=link;
                       // window.open(link);
                        //window.open('https://activity.m.duiba.com.cn/chome/index?from=login&spm=53694.1.1.1','_blank');
                    })
            }
        }
        $scope.closedActiveDailog=function(){
            $scope.activeDuiDailogStatus=false;
        }
        $scope.goTo=function(){
            var  rs = "main.newactivelist-" + JSON.stringify({'kind':$scope.currentIndex,activenavbar:$scope.activenavbar});
            var token=localStorageService.get('token');
            var isbind = localStorageService.get('isbind');
            var openID = localStorageService.get('openid');
            var ua = window.navigator.userAgent.toLowerCase();
            if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                if (isbind != 1 && openID != '') {
                    localStorageService.set('cleartoken', token);
                    location.href = "#/main/register/" + encodeURIComponent(rs);
                    localStorageService.set('token', '');
                    return false
                }
            }
            if (!localStorageService.get('token')) {
                var  rs = "main.newactivelist-" + JSON.stringify({'kind':$scope.currentIndex,activenavbar:$scope.activenavbar});
                var ua = window.navigator.userAgent.toLowerCase();
                if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                    location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                        "appid=wxda73ac8ac7af1261&" +
                        "redirect_uri=http%3A%2F%2Ftest.djx.m.opentickets.cn%2Foauth%2Findex" +
                        "&response_type=code&scope=snsapi_userinfo&state="+encodeURIComponent(rs)+"&connect_redirect=1#wechat_redirect";
                } else {
                    console.log(encodeURIComponent(rs));
                    location.href = "#/main/login/"+encodeURIComponent(rs);
                }
                return false;
            }
        }
        $scope.goTojump=function(){  //跳转
            if($scope.AcTivityBanner.picjump!=undefined&&$scope.AcTivityBanner.picjump!=null&&$scope.AcTivityBanner.picjump!=''){
                if(_czc!=undefined&&_czc!=null&&_czc!='') {
                    _czc.push(["_trackEvent", 'h5-活动广告', '活动广告', '点击量', 'activity']);
                }
                window.open($scope.AcTivityBanner.picjump);
            }
        }
        $scope.getBarNavs=function(){
            // 分类列表
            $api.get("app/activity/classifylist",{}, true)
                .then(function(ret){
                    var data=ret.data;
                    // console.log(ret);
                    $scope.barNavs=data;
                    // $scope.barNavs
                    $scope.currentClassfyId=$scope.barNavs[$scope.currentIndex].key;
                    refresh();
                },function(err){
                    $alert.show(err);
                });
        }
        var h=$(window).height()-3.8*parseFloat(document.documentElement.style.fontSize)+"px";
        $("#wrapper_container").height(h);
        $("#activitysAdvs").height($(window).width()/3.75+'px');
        var refresh=function(){
            $scope.Activitystatus=false;
            $scope.raisegoods = new $lazyLoader("app/activity/activityList",{
                classifyType:$scope.currentClassfyId,   //分类id
                activityState:$scope.activenavbar  //状态
            },true);
            $scope.raisegoods.nextPage();
        }
        //初始化
        $scope.getBarNavs();
        $scope.getAcTivityBanner();
        //是否显示 活动状态
        $scope.showActivitystatus=function(){
            if( $scope.Activitystatus){
                $scope.Activitystatus=false;
            }else{
                $scope.Activitystatus=true;
            }
        }
        //关闭广告
        $scope.closedActivitysAdvs=function(){
            $scope.closedActivitysAdvsStatus=false;
        }
        $scope.switchactivebar=function(index){
            $scope.activenavbar=index;
            $state.go('main.newactivelist',{'kind':$scope.currentIndex,activenavbar:$scope.activenavbar});
        }
        $scope.changeCityShow=function(){
            if($scope.cityStatus){
                $scope.cityStatus=false;
            }else{
                $scope.cityStatus=true;
            }
        }
        $scope.getCureenCity=function(){
            $api.get("app/city/list",{}, true)
                .then(function(ret){
                    $scope.saiCities=ret.data;
                })
        }
        $scope.getCureenCity();
        //选中筛选的条件
        $scope.changesnav=function(idnx,text,cid){
                $scope.cityStatus=false;
                $scope.cityid=cid;
                $scope.cityName=text;
            localStorageService.set('cityname', text);
            localStorageService.set('cityid',cid);//全国默认
                refresh();
        }
        $scope.sortscity=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','R','S','T','U','V','W','X','Y','Z'];
        $scope.gotoBottom = function(id) {
            //   console.log(id);
            // 将location.hash的值设置为
            // 你想要滚动到的元素的id
            $location.hash(id);

            // 调用 $anchorScroll()
            $anchorScroll();
        };
        $scope.bartotla=function(total,count){
            if(count==undefined||count==null||count==''){
                count=0;
            }
            if(count>=total){
                count=total;
            }
            var bar=parseInt(count/total*100);
            $scope.barstyle = {
                "width":bar+'%' ,
                "background":"#e72e62"
            }
            return $scope.barstyle;
        }
        $scope.bartotlashu=function(total,count){
            if(count==undefined||count==null||count==''){
                count=0;
            }
            // if(count>=total){
            //     count=total;
            // }
            $scope.barshu=parseInt(count/total*100)+'%';
            return $scope.barshu;
        }
        // 点击导航栏
        $scope.switch = function(kind,index){
            //console.log($stateParams);
            $scope.currentClassfyId=kind;
            $scope.currentIndex=index;
            if(index!=0){
                $scope.showActivitysBidStatus=false;
                $scope.closedActivitysAdvsStatus=false;
            }else{
                $scope.showActivitysBidStatus=true;
                $scope.closedActivitysAdvsStatus=true;
                if (!$scope.noPic) {
                    // console.log(_czc );
                    if (_czc != undefined && _czc != null && _czc != '') {
                        _czc.push(["_trackEvent", 'h5-活动广告', '活动广告', '展示量', 'activity']);
                    }
                }
            }
            // console.log(kind);
            // refresh();
            if(_czc!=undefined&&_czc!=null&&_czc!='') {
                _czc.push(["_trackEvent", 'h5-活动导航栏-' + index, index, '查看', 'activity']);
            }
            $state.go('main.newactivelist',{'kind':$scope.currentIndex,activenavbar:$scope.activenavbar});
        };

        // $scope.$on("navFinishRender",function(){
        //     $('#demo05').navbarscroll({
        //         defaultSelect:$scope.currentIndex,
        //
        //     });
        // })
        $scope.$on('ngRepeatFinished', function(ngRepeatFinishedEvent) {
            $('#demo05').navbarscroll({
                defaultSelect:$scope.currentIndex,

            });
        })
    });